package com.stylefeng.guns.rest.modular.attent.controller;

import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.modular.attent.service.CareService;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleListResponse;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleRequest;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.CatalogyListRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by ASUS-PC on 2018/9/8.
 */
@RestController
@RequestMapping("/care")
public class CareController {
    @Autowired
    private CareService careService;


    /**
     * 点赞或取消点赞
     * @param request
     * @return
     */
    @RequestMapping(value = "/artitleCare", method = RequestMethod.POST)
    public ResponseEntity<?> getUserArticleList(@RequestBody ArticleRequest request){
        if(request.getUserId()==0 || request.getArticleId()==0 || request.getType()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        //点赞
        if(request.getType()==1){
            boolean state = careService.insertCare(request);
            if(state){
                return ResponseEntity.ok(CommonResponse.ok(null));
            }else {
                throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
            }
        }else {//取消点赞
            boolean state = careService.deleteAttent(request);
            if(state){
                return ResponseEntity.ok(CommonResponse.ok(null));
            }else {
                throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
            }
        }
    }

    /**
     * 获取我点赞/喜欢的文章列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/careList", method = RequestMethod.POST)
    public ResponseEntity<?> getUserArticleCareList(@RequestBody CatalogyListRequest request){
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        List<ArticleListResponse> mList = careService.artitleCareList(request);
        return ResponseEntity.ok(CommonResponse.ok(mList));

    }

    /**
     * 获取别人给我文章的点赞
     * @param request
     * @return
     */
    @RequestMapping(value = "/myCareList", method = RequestMethod.POST)
    public ResponseEntity<?> getMyArticleCareList(@RequestBody CatalogyListRequest request){
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        return ResponseEntity.ok(CommonResponse.ok(careService.getMyCareList(request)));
    }
}
